package com.shen.thread.syn;

//死锁
public class DeadLock {
    public static void main(String[] args) {
        new Makeup(0, "boy").start();
        new Makeup(1, "girl").start();
    }
}

//镜子
class Mirror {

}

//口红
class Lipstick {

}

class Makeup extends Thread {
    //需要的资源只有一份
    static Mirror mirror = new Mirror();
    static Lipstick lipstick = new Lipstick();

    int choose;
    String name;

    Makeup(int choose, String name) {
        this.choose = choose;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        if (choose == 0) {
            synchronized (mirror) {
                System.out.println(this.name + "获得镜子的锁");
                Thread.sleep(1000);

            }
            synchronized (lipstick) {
                System.out.println(this.name + "获得口红的锁");
            }
        } else {
            synchronized (lipstick) {
                System.out.println(this.name + "获得口红的锁");
                Thread.sleep(2000);

            }
            synchronized (mirror) {
                System.out.println(this.name + "获得镜子的锁");
            }
        }
    }
}